package com.mindata.blockchain.socket.packet;

import java.util.HashMap;
import java.util.Map;

/**
 * packetType大于0时是请求类型，小于0时为响应类型
 *
 * @author wuweifeng wrote on 2018/3/9.
 */
public enum PacketType {

    HEART_BEAT(new Byte("0"), "心跳包"),
    GENERATE_COMPLETE_REQUEST(new Byte("1"), "已生成新的区块"),
    GENERATE_COMPLETE_RESPONSE(new Byte("-1"), "已生成新的区块回应"),

    GENERATE_BLOCK_REQUEST(new Byte("2"), "请求生成block"),
    GENERATE_BLOCK_RESPONSE(new Byte("-2"), "同意、拒绝生成"),

    TOTAL_BLOCK_INFO_REQUEST(new Byte("3"), "获取所有block信息"),
    TOTAL_BLOCK_INFO_RESPONSE(new Byte("-3"), "我的所有块信息"),

    FETCH_BLOCK_INFO_REQUEST(new Byte("4"), "获取一个block信息"),
    FETCH_BLOCK_INFO_RESPONSE(new Byte("-4"), "获取一块信息响应"),

    NEXT_BLOCK_INFO_REQUEST(new Byte("5"), "获取下一个区块的信息"),
    NEXT_BLOCK_INFO_RESPONSE(new Byte("-5"), "获取下一个区块的信息"),

    ADD_INSTRUCTION_REQUEST(new Byte("6"), "广播区块指令"),
    ADD_INSTRUCTION__RESPONSE(new Byte("-6"), "广播区块指令回应"),

    COMMIT_RAFT_BLOCK_REQUEST(new Byte("7"), "广播提交RAFT区块生成指令"),
    COMMIT_RAFT_BLOCK_RESPONSE(new Byte("-7"), "广播提交RAFT区块生成指令回应"),

    PBFT_VOTE(new Byte("10"), "pbft投票");

    private Byte key;
    private String value;

    PacketType(Byte key, String value) {
        this.key = key;
        this.value = value;
    }

    public Byte getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }


    private static final Map<Byte, PacketType> ENUMMAP = new HashMap<Byte, PacketType>();

    static {
        for (PacketType packetType : PacketType.values()) {
            ENUMMAP.put(packetType.getKey(), packetType);
        }
    }

    public static PacketType fromValue(Byte key) {
        return ENUMMAP.get(key);
    }
}
